package com.neusoft.yuejian_01.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.activity.AddSetPictureActivity;
import com.neusoft.yuejian_01.adapter.SetPicturelistAdapter;
import com.neusoft.yuejian_01.javabean.SetPictureBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.SetPictureModel;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class SetPictureFragment extends Fragment implements RetrofitListener{
    List<String> list = new ArrayList<String>();
    private RecyclerView recyclerView;
    private  View view=null;
    private Spinner mySpinner;
    private ImageView BtnPost;
    private Context context;
    private  TextView myTextView;
    private ArrayAdapter<String> adapter;
    private FragmentManager manager;
    private   MeFragment meFragment;
    public SetPictureFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_set_picture,container,false);
        BtnPost=(ImageView) view.findViewById(R.id.addPost);
        recyclerView = (RecyclerView) view.findViewById(R.id.picture_recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        list.add("全部晒图");
        list.add("大师晒图");
        list.add("我的俱乐部");
        myTextView=(TextView) view.findViewById(R.id.shaitu);
        mySpinner = (Spinner)  view.findViewById(R.id.setpiclist);
        adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//晒图跳转
     BtnPost.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
           Intent intent =new Intent(getActivity(),AddSetPictureActivity.class);
             startActivity(intent);
                    }
                });
//下拉式，显示不同的晒图列表大师晒图，我的俱乐部，全部晒图
        mySpinner.setAdapter(adapter);

        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // TODO Auto-generated method stub

              if("大师晒图" == adapter.getItem(arg2)){

                  myTextView.setText( adapter.getItem(arg2));
                  initData();

            }

              else if ("我的俱乐部"==adapter.getItem(arg2))

              {
                  myTextView.setText( adapter.getItem(arg2));
                  initDataaa();
              }



                 else if ("全部晒图"==adapter.getItem(arg2))

               {
                   myTextView.setText( adapter.getItem(arg2));
               initDataa();
                }

            }


            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
      return view;
    }

    //获取大师晒图列表信息
    private void initData() {
        SetPictureModel setPictureModel = new SetPictureModel();
        setPictureModel.getDashiSetPicturelist(7,this);
 }
    //获取全部晒图列表信息
    private void initDataa() {
        SetPictureModel setPictureModel = new SetPictureModel();
        setPictureModel.getSetPicturelist(this);
    }
    //获取我的俱乐部晒图列表信息
    private void initDataaa() {
        SetPictureModel setPictureModel = new SetPictureModel();
        setPictureModel.getmyCollectlist(getC_id(),this);
    }

    private void replace(Fragment fragment) {
        manager = getChildFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.setpiocinf, fragment);
        transaction.commit();
    }


    public int getUser_id()
    {
        SharedPreferences sp;
        sp =getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getInt("u_id", -1);
    }

    public int getC_id()
    {
        SharedPreferences sp;
        sp =getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getInt("c_id", -1);
    }


    @Override
    public void onSuccess(Object object, int flag) {
        List<SetPictureBean> SetPicture = (List<SetPictureBean>)object;
        switch (flag){
            case Const.DashiSetPicturelist:
                recyclerView.setAdapter(new SetPicturelistAdapter(getActivity(),SetPicture));
                break;
            case Const.SetPicturelist:
                recyclerView.setAdapter(new SetPicturelistAdapter(getActivity(),SetPicture));
                break;
            case Const.MyCollectlist:
                recyclerView.setAdapter(new SetPicturelistAdapter(getActivity(),SetPicture));
                break;

        }

    }

    @Override
    public void onFail() {

    }
}
